package com.session;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

@Controller
@RequestMapping(value = "/session")
public class IndexController {

	@RequestMapping("/login")
	@ResponseBody
	public String login(HttpServletRequest request, String username , String password) {
		
		if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
			return "faild";
		}
		
		JSONObject json = new JSONObject();
		json.put("userid", 1);
		json.put("username", username);
		json.put("password", password);
		
		request.getSession().setAttribute("userInfo", JSON.toJSONString(json));
		
		return "success";
	}
	
	@RequestMapping("/userCenter")
	public ModelAndView userCenter(HttpServletRequest request) {
		ModelAndView mv = new ModelAndView();
		
		String userInfo = request.getSession().getAttribute("userInfo").toString();
		System.out.println(userInfo);
		JSONObject json = JSONObject.parseObject(userInfo);
		
		mv.addObject("user", json);
		mv.setViewName("userinfo");
		
		return mv;
	}
	
}
